/**
 * 簿记交收
 * @author zhanghaitao(2021-12-15)
 */
import api from '../../api';
import { message } from 'antd';
import config from '../../comps/business/tradingMarket/config';
import typeConfig from '../../comps/business/tradingDirect/config';
export default {
  namespace: 'bookkeepingBehaviorM',
  state: {
    columns: [
      {
        title: '日期',
        dataIndex: 'createdTs',
        key: 'createdTs',
        type: 'date',
        width: 120,
      },
      {
        title: '产品',
        dataIndex: 'product',
        key: 'product',
        width: 160,
        render: (val, record) => {
          return `${record.productCode}-${record.productName}`;
        },
      },
      {
        title: '组合',
        dataIndex: 'portfolioInfo',
        key: 'portfolioInfo',
        width: 160,
        render: (val, record) => {
          return `${record.portfolioInfoCode}-${record.portfolioInfoName}`;
        },
      },
      {
        title: '证券代码',
        dataIndex: 'tradingcode',
        key: 'tradingcode',
        width: 150,
      },
      {
        title: '证券名称',
        dataIndex: 'tradingName',
        key: 'tradingName',
        width: 150,
      },
      {
        title: '交易市场',
        dataIndex: 'exchangecode',
        key: 'exchangecode',
        width: 120,
        render: (val, record) => {
          let findvalue = config.filter((item) => {
            return item.value == val;
          });
          if (findvalue && findvalue.length > 0) {
            return findvalue[0].title;
          } else {
            return '--';
          }
        },
      },
      {
        title: '资产类型',
        dataIndex: 'type',
        key: 'type',
        width: 140,
        render: (val, record) => {
          let findType = typeConfig.filter((item) => {
            return item.value == val;
          });
          if (findType && findType.length > 0) {
            return findType[0].title;
          } else {
            return '--';
          }
        },
      },
      {
        title: '交易方向',
        dataIndex: 'direction',
        key: 'direction',
        width: 90,
        render: (val, record) => {
          // 根据业务类型，翻译交易方向
          let findtypes = typeConfig.filter((item) => {
            return item.value == record.type;
          });
          if (findtypes && findtypes.length > 0) {
            let findorderSide = findtypes[0].tradingDirect.filter((item) => {
              return item.value == val;
            });
            if (findorderSide && findorderSide.length > 0) {
              return findorderSide[0].title;
            } else {
              return '--';
            }
          } else {
            return '--';
          }
        },
      },
      {
        title: '交易金额',
        dataIndex: 'settlementAmount',
        key: 'settlementAmount',
        type: 'money',
        width: 110,
      },
      {
        title: '交收日期',
        dataIndex: 'settlementDate',
        key: 'settlementDate',
        type: 'date',
        width: 120,
      },
      {
        title: '状态',
        dataIndex: 'instructionStatus',
        key: 'instructionStatus',
        width: 90,
        render: (val, record) => {
          switch (val) {
            case '6':
              return '未交收';
            case '4':
              return '已交收';
            case '7':
              return '已交收';
            case '8':
              return '到期交收';
            case '11':
              return '到期未交收';
            default:
              return '--';
          }
        },
      },
    ],
  },
  effects: {
    //获取簿记行为交收列表
    *getBehavioeList({ payload, callback }, { call, put, select }) {
      const res = yield call(api.bookkeepingSettle.getBehavioeList, payload);
      if (res) {
        callback({
          pageInfo: {
            total: res.total,
            currentPage: res.current,
            pageSize: res.pageSize,
          },
          dataSource: res.data,
        });
      }
    },
    //单个交收
    *getOneInstruction({ payload, callback }, { call, put }) {
      const response = yield call(api.bookkeepingSettle.getOneInstruction, payload);
      if (response && response.success) {
        callback('success');
        message.success('交收成功');
      } else {
        callback('fail');
        message.error(response.message || '交收失败!');
      }
    },
    //批量交收
    *getBatchInstruction({ payload, callback }, { call, put }) {
      const response = yield call(api.bookkeepingSettle.getBatchInstruction, payload);
      if (response && response.success) {
        callback('success');
        message.success('交收成功');
      } else {
        callback('fail');
        message.error(response.message || '交收失败!');
      }
    },
    //回溯
    *getBehaviorInstructionHS({ payload, callback }, { call, put }) {
      const response = yield call(api.bookkeepingSettle.getBehaviorInstructionHS, payload);
      if (response && response.success) {
        callback('success');
        message.success('回溯成功');
      } else {
        callback('fail');
        message.error(response.message || '回溯失败!');
      }
    },
  },
  reducers: {
    updateData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
  subscriptions: {},
};
